package com.sdp.wx.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_labor_standard.entity.WxLaborStandard;
import com.sdp.wx.db.wx_labor_standard.service.IWxLaborStandardService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-27
 */
@RestController
@RequestMapping("/WxLaborStandardController")
public class WxLaborStandardController {

	@Autowired
	private IWxLaborStandardService laborStandardService;
	
	@Autowired
	private Query query;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(laborStandardService, WxLaborStandard.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxLaborStandard laborStandard) {
		if(laborStandard == null) {
			return Result.error("提交信息为空");
		}
		if(StringUtils.isEmpty(laborStandard.getKeyId())) {
			laborStandard.setCreateTime(LocalDateTime.now());
			laborStandardService.save(laborStandard);
		} else {
			laborStandardService.updateById(laborStandard);
		}
		return Result.ok(laborStandard);
	}
	
	@GetMapping(value = "/del")
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		laborStandardService.removeById(keyId);
		return Result.ok();
	}
	
}

